﻿#region License Info
//Component of Cronos Package, http://www.codeplex.com/cronos
//Copyright (C) 2009 Anthony Brockwell

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#endregion

using System;
using System.Windows.Forms;
using ABMath.ModelFramework;
using ABMath.ModelFramework.Data;
using ABMath.ModelFramework.Visuals;
using ABMath.ModelFramework.Transforms;
using ExtraControlLibrary.DirectedGraphs;

namespace ExtraControlLibrary.VisualControls
{
    public partial class TransformationControl : UserControl, IUpdateable
    {
        private readonly TimeSeriesTransformation transform;
        private readonly DirectedGraphViewer dagViewer;

        private bool showLag0;
        public bool ShowLag0
        {
            get { return showLag0; } 
            protected set
            {
                showLag0 = value;
                tsPlotControl.ShowLag0 = value;
            }
        }

        public TransformationControl(TimeSeriesTransformation transform, DirectedGraphViewer dagViewer,
                                     bool showLag0)
        {
            this.transform = transform;
            this.dagViewer = dagViewer;
   
            InitializeComponent();

            var ief = transform as IExtraFunctionality;
            if (ief != null)
                extraFunctionPanel1.FunctionObject = ief;

            propertyGrid1.SelectedObject = transform;
            ShowLag0 = showLag0;

            UpdatePlot();
        }

        private void UpdatePlot()
        {
            if (!transform.IsValid)
                return;
            var tsout = transform.GetOutput(0) as TimeSeries;
            var mvtsOut = transform.GetOutput(0) as MVTimeSeries;
            var ltsout = transform.GetOutput(0) as Longitudinal;
            if (tsout != null)
                tsPlotControl.SetVisual(new TSPlotVisual(tsout));
            else if (mvtsOut != null)
                tsPlotControl.SetVisual(new TSPlotVisual(mvtsOut));
            else if (ltsout != null)
                tsPlotControl.SetVisual(new TSPlotVisual(ltsout));
            else tsPlotControl.SetVisual(null);
            tsPlotControl.Refresh();
            var s = transform.GetLastComputationInfo();
            if (s != null)
                infoTextBox.Text = s;
        }

        private void recomputeButtonClick(object sender, EventArgs e)
        {
            if (transform.AllInputsValid())
            {
                transform.Recompute();
                // now also redraw the particular transform in the dagviewer since it may have changed
                dagViewer.RedrawButtonFor(transform);
                dagViewer.UpdateFromNodeContaining(transform);

                UpdatePlot();
                propertyGrid1.Refresh();
            }
            else
                MessageBox.Show("Unable to recompute transform, not all inputs are valid.", "Error");
        }

        public void DataHasChanged()
        {
            throw new ApplicationException("This function should not be called!");
        }

        private void extraFunctionPanel1_FunctionCompleted(object sender, ExtraFunctionPanel.ExtraFunctionArgs args)
        {
            dagViewer.RedrawButtonFor(transform);
            dagViewer.UpdateFromNodeContaining(transform);
            UpdatePlot();
            propertyGrid1.Refresh();
        }
    }
}